package com.stedu.mall.userservice.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.common.utils.JwtUtil;
import com.stedu.mall.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserService userService;
    @GetMapping("/captcha")
    public RespBean captcha(){
        LineCaptcha captcha=new LineCaptcha(150,50,5,10 );

        String key= IdUtil.getSnowflakeNextIdStr();

        redisUtil.set(key, captcha.getCode(), 60);

        String imageBase64Data=captcha.getImageBase64Data();

        HashMap<String,Object> map=new HashMap<>();
        map.put("key", key);
        map.put("imageBase64Data", imageBase64Data);
        return  RespBean.ok("生成验证码成功",map);
    }
    @PostMapping("/login")
    public RespBean login(String username,String password,String key,String captchaInput) throws SteduException {
        //判断验证码是否正确-从Redis中查询验证码进行判断
        String captcha =(String)redisUtil.get(key);
        if(captcha == null || !captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误");
        }
        User user=userService.login(username, password);
        HashMap<String,Object>map=new HashMap<>();
        map.put("id",user.getId());
        map.put("username",user.getUsername());
        map.put("role","user");
        String jwtStr= JwtUtil.generateJwt(map);
        //登录
        //生成token
        //返回token给用户a
        return RespBean.ok("登录成功",jwtStr);
    }
    //注册
@PostMapping("/reg")
    public RespBean reg(String username,String password,String key, String captchaInput) throws SteduException {
    String captcha =(String) redisUtil.get(key);
    if(captcha==null || !captcha.equalsIgnoreCase(captchaInput)){
        throw new SteduException("验证码错误，请确认后重新输入");
    }
    User user = new User();
    user.setUsername(username);
    user.setPassword(password);
    //注册
    userService.insert(user);
    return RespBean.ok("注册成功");
}

    //根据id查询信息
    @GetMapping("/info")
    public RespBean getLogin(@RequestHeader("token") String token){
        //token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //
        Integer id = (Integer)map.get("id");
        User user= userService.selectById(id);
        String idCard = user.getIdCard();
        String partialIdCard = idCard!= null? idCard.substring(0, 3) + "****" + idCard.substring(idCard.length() - 4) : null;
        user.setIdCard(partialIdCard);
        return RespBean.ok("",user);
    }

    @PutMapping("/chgInfo")
    public RespBean update(@RequestBody User user, @RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        user.setId(id);
        userService.updateInfo(user);
        return RespBean.ok("修改成功");
    }
    @PutMapping("/updateMoney")
    public RespBean updateMoney(@RequestBody User user, @RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        user.setId(id);
        userService.updateMoney(user);
        return RespBean.ok("充值成功");
    }
    @PutMapping("/updatePayPwd")
    public RespBean updatePayPwd(String newPwd, String oldPwd, @RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        userService.updatePayPwd(id, newPwd, oldPwd);
        return RespBean.ok("修改成功");
    }
    //修改密码
    @PutMapping("/updatePwd")
    public RespBean updatePwd(String newPwd, String oldPwd, @RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        userService.updatePwd(id, newPwd, oldPwd);
        return RespBean.ok("修改成功");
    }
    @PutMapping("/updateSetPwd")
    public RespBean updateSetPwd(String loginPwd, String payPwd, @RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        userService.updateSetPwd(id, loginPwd, payPwd);
        return RespBean.ok("设置成功");
    }

    //认证
    @PutMapping("/updateCertifying")
    public RespBean updateCertifying( @RequestBody User user, @RequestHeader("token") String token) throws SteduException {
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        user.setId(id);
        userService.updateCertifying(user);
        return RespBean.ok("认证成功");
    }
    @GetMapping("/All")
    public RespBean selectByUser(User condition, Integer pageNum, Integer pageSize)  {
        pageNum = pageNum==null ? 1:pageNum;
        pageSize = pageSize==null ? 5:pageSize;
        PageInfo<User> pageInfo = userService.condition(condition, pageNum, pageSize);
        return  RespBean.ok("查询成功",pageInfo);
    }
    //重置密码
    @PutMapping("/{id}")
    public RespBean resetPwd( @PathVariable("id") Integer id) {
        User user = userService.selectById(id);
        user.setPassword("123456");
        userService.resetPwd(user);
        return RespBean.ok("重置成功");
    }
    @PutMapping("/Status")
    public RespBean updatePwd( @RequestBody User user) throws SteduException {
        userService.update(user);
        return RespBean.ok("修改成功");
    }
    }